﻿using System;
using System.Data;
using System.Data.OracleClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public partial class SysManage_LogQuery : UserPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LogList.Attributes.Add("SortExpression", "LOG_DATE");
            LogList.Attributes.Add("SortDirection", "DESC");
            SetLogPager();
        }
    }

    protected void LogPager_PageChanged(object sender, EventArgs e)
    {
        SetLogPager();
    }

    protected void LogList_Sorting(object sender, GridViewSortEventArgs e)
    {
        string sortExpression = e.SortExpression.ToString();
        string sortDirection = "DESC";

        if (sortExpression == LogList.Attributes["SortExpression"])
        {
            sortDirection = (LogList.Attributes["SortDirection"].ToString() == sortDirection ? "ASC" : "DESC");
        }

        LogList.Attributes["SortExpression"] = sortExpression;
        LogList.Attributes["SortDirection"] = sortDirection;
        SetLogPager();
    }

    private void SetLogPager()
    {
        PagedDataSource pds = new PagedDataSource();
        DataTable dt = GetLog();
        LogPager.RecordCount = dt.Rows.Count;
        pds.DataSource = dt.DefaultView;
        pds.AllowPaging = true;
        pds.PageSize = LogList.PageSize;
        pds.CurrentPageIndex = LogPager.CurrentPageIndex - 1;
        LogList.DataSource = pds;
        LogList.DataBind();
    }

    private DataTable GetLog()
    {
        string sqlStr = "select log_obj,log_date,log_msg,log_level from u_log_t order by log_date desc";
        DataTable dt = OracleHelper.ExecuteDataSet(sqlStr, CommandType.Text, null).Tables[0];
        if ((!string.IsNullOrEmpty(LogList.Attributes["SortExpression"])) && (!string.IsNullOrEmpty(LogList.Attributes["SortDirection"])))
        {
            dt.DefaultView.Sort = string.Format("{0} {1}", LogList.Attributes["SortExpression"], LogList.Attributes["SortDirection"]);
        }
        return dt;
    }

    protected void LogList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            foreach (DataControlField dataControlField in LogList.Columns)
            {
                if (dataControlField.SortExpression.Equals(LogList.Attributes["SortExpression"]))
                {
                    Label label = new Label();
                    label.Text = LogList.Attributes["SortDirection"].Equals("ASC") ? "▲" : "▼";
                    e.Row.Cells[LogList.Columns.IndexOf(dataControlField)].Controls.Add(label);
                }
            }
        }

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#D5E5EA'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");
        }
    }
}
